<?php

namespace app\common\service\platform_manage\log;

use app\common\exception\CustomException;
use app\common\model\Ip;
use app\common\service\BaseService;
use app\common\traits\BaseServiceTrait;
use think\Db;

class ServerOptionHistoryService extends BaseService
{
    use BaseServiceTrait;

    /**
     * @name 列表
     * @param $params
     * @return array
     * @author wx
     * @date 2020/10/21 16:17
     */
    public function list($params)
    {
        $db = Db::name('server_option_history');

        //条件筛选
        if(isset($params['type']) && !empty($params['type'])){
            $db->where('soh.type',$params['type']);
        }
        if(isset($params['content']) && !empty($params['content'])){
            $db->where('soh.content','like','%'.$params['content'].'%');
        }
        if(isset($params['ctime']) && !empty($params['ctime'])){
            $start_time = $params['ctime'].' 00:00:00';
            $end_time = $params['ctime'].' 23:59:59';
            $time_stamp_start = strtotime($start_time);
            $time_stamp_end = strtotime($end_time);
            $db->where('soh.ctime','between',[$time_stamp_start, $time_stamp_end]);
        }
        if(isset($params['user_name']) && !empty($params['user_name'])){
            $db->where('su.show_name','like','%'.$params['user_name'].'%');
        }
        if(isset($params['ip']) && !empty($params['ip'])){
            $db->where('soh.ip','like','%'.$params['ip'].'%');
        }
        if(isset($params['status']) && is_numeric($params['status'])){
            $db->where('soh.status', $params['status']);
        }
        if(isset($params['is_detail']) && is_numeric($params['is_detail'])){
            $db->where('soh.is_detail', $params['is_detail']);
        }

        $field = 'soh.id, soh.type, soh.content, soh.ctime, soh.address,
         soh.ip, soh.status, su.show_name as user_name, soh.is_detail';
        //查询查询数据
        $data = $db->alias('soh')
            ->join('server_user su','soh.user_id = su.id')
            ->order('soh.ctime desc')
            ->field($field)
            ->paginate()->toArray();

        return $data;
    }

    /**
     * @name 查询数据
     * @author wx
     * @date 2020/10/26 10:34
     */
    public function findData($condition, $field = '*')
    {
        return Db::name('server_option_history')->where($condition)->field($field)->find();
    }

    /**
     * @name 添加用户操作日志
     * @author wx
     * @date 2020/10/21 16:22
     */
    public function addUserLog()
    {
        global $logArray;
        global $errCode;
        global $errMsg;

        if(empty($logArray) || !isset($logArray['content'])){
            return true;
        }

        $logContent = $logArray['content'];

        $status = 1;
        if($errCode != $this->CODE_SUCCESS){
            $status = 0;
            $logContent = $logArray['content'] . ' [ 失败原因：'.$errMsg.']';
        }

        //验证用户
        $userId = \think\facade\Request::param('user_id');

        if(empty($userId) ){
            return true;
        }

        //1. 查询ip对应的地址address
        $addressR = $_SERVER['DOCUMENT_ROOT'].'/static/IP/qqzeng.dat';
        if(!file_exists($addressR))
        {
            // throw new CustomException('未找到IP地址对应物理文件', 502);
            return false;
        }
        $ipServer = new Ip($addressR);
        $ipInfo = $ipServer->getlocation($_SERVER['REMOTE_ADDR']);
        if($ipInfo){
            $address = $ipInfo['country'].$ipInfo['province'].$ipInfo['city'].$ipInfo['area'];
        }else{
            $address = '保留';
        }

        //构造数据
        $addLogData['user_id'] = $userId;
        $addLogData['type'] = 1;
        $addLogData['content'] = $logContent;
        $addLogData['address'] = $address;
        $addLogData['ip'] = $_SERVER['REMOTE_ADDR'];
        $addLogData['status'] = $status;
        $addLogData['ctime'] = time();

        if(isset($logArray['old_info'])){
            $addLogData['old_info'] = $logArray['old_info'];

            $addLogData['is_detail'] = $status;
        }
        if(isset($logArray['new_info'])){
            $addLogData['new_info'] = $logArray['new_info'];
            $addLogData['is_detail'] = $status;
        }

        //添加数据
        Db::name('server_option_history')->insert($addLogData);
    }
}